<?
/**
 * Cr�er un pdf avec FPDF
 */
if(!class_exists("PDF")) include_once LIB_PATH."/helpers/fpdf/WriteTag.php";
if(!class_exists("PDFI"))include_once LIB_PATH."/helpers/fpdf/fpdi.php";
class PdfFile {
   /**
    * Contructeur
    * @param int $pageWidth Largeur de la page (defautl 210mm)
    * @param int $pageHeight Hauteur de la page (default 297mm)
    * @param int $lineHeight hauteur d'une ligne (default 5mm)
    * @param int $x X de d�part
    * @param int $y Y de d�part
    * @param 
    */
    public function __construct($Pagewidth=210,$Pageheight=297,$lineHeight=5,$x=0,$y=0){
	   define('EURO',chr(128)); 
	   $this->y = $x;
	   $this->x = $y;
	   $this->pageWidth = $Pagewidth;
	   $this->pageHeight = $Pageheight;
	   $this->lineHeight = $lineHeight;
	   $this->fontHeight = 10;
	   // Fpdf
	   $PDF=new PDF();
	   $PDF->SetFont('Helvetica','',$this->fontHeight);
	   $PDF->AddPage();
	   //$PDF->Image(dirname(__FILE__)."/../images/courrier.jpg",0,0,210,297);
	   $PDF->SetAutoPageBreak('on', 0.5);
	   // Tags
	   $PDF->SetStyle("ui", "Helvetica", "UI", $this->fontHeight, "0, 0, 0");
	   $PDF->SetStyle("col", "Helvetica", "B", $this->fontHeight, "0, 129, 0");
	   $PDF->SetStyle("b", "Helvetica", "B", $this->fontHeight, "0, 0, 0");
	   $PDF->SetStyle("bu", "Helvetica", "BU", $this->fontHeight, "0, 0, 0");
	   $PDF->SetStyle("top", "Helvetica", "BI", $this->fontHeight, "54, 95, 145");
	   $PDF->SetStyle("gray", "Helvetica", "BI", $this->fontHeight, "190, 190, 190");
	   $this->pdf = $PDF;
    }
    /**
     * Taille de la police
     *@param int $height
     */
    public function setFontHeight($height){
	   $this->fontHeight = $height;
	   $this->pdf->SetFont('Helvetica','',$this->fontHeight);
	   $this->pdf->SetStyle("ui", "Helvetica", "UI", $this->fontHeight, "0, 0, 0");
	   $this->pdf->SetStyle("col", "Helvetica", "B", $this->fontHeight, "220, 220, 220");
	   $this->pdf->SetStyle("b", "Helvetica", "B", $this->fontHeight, "0, 0, 0");
	   $this->pdf->SetStyle("bu", "Helvetica", "BU", $this->fontHeight, "0, 0, 0");
	   $this->pdf->SetStyle("top", "Helvetica", "BI", $this->fontHeight, "54, 95, 145");
	   $this->pdf->SetStyle("gray", "Helvetica", "N", $this->fontHeight, "190, 190, 190");
	   
    }
    /**
     * Met une image en arri�re plan
     * @param string $image url de l'image
     */
    public function setBackground($image){
	   $this->backgroundImage = $image;
	   $this->pdf->Image($image,0,0,$this->pageWidth,$this->pageHeight);
    }
    /**
     * Met les marges de la page
     * @param int $left marge � gauche
     * @param int $right marge a droite
     */
    public function setMarges($left,$right){
	   $this->pdf->SetLeftMargin($left);
	   $this->pdf->SetRightMargin($right);
    }
    public function Write($text){
    	$this->pdf->Write(5,$text);
    }
    /**
     * Insert du texte
     * @param string $text 
     */
    public function afficheText($text,$ret=true,$upY=5){
	   $text = str_replace("<br />","<br/>",$text);
	   $text = str_replace("<br>","<br/>",$text);
	   $aText = explode("<br/>",$text);
	   if(count($aText)>1){
		  foreach($aText as $t){
			 $this->afficheText($t,$ret,$upY);
		  }
	   }else{
		  $text = $aText[0];
		  $text = utf8_decode($text);
		  $this->pdf->SetXY($this->x,$this->y);
		  $this->pdf->WriteTag($this->width,$this->lineHeight,$style.$text.$fstyle,0,"J");
		  if($ret) $this->upY($upY);
	   }
    }
    public function afficheTh($width,$text,$align="C"){
	   // Padding : left, top, bottom, right"
	   $text = utf8_decode($text);
	   $this->pdf->SetXY($this->x,$this->y);
	   $this->pdf->WriteTag($width,$this->lineHeight,"<b>$text</b>",1,$align,1,"1, 0, 0, 1");
// 	   print_r($this->pdf);die;
	   $this->upX($width);
    }
    public function afficheTd($width,$text,$align="L",$border=1,$height=""){
	   if($height == "") $height = $this->lineHeight;
	   $text = str_replace("<br>",PHP_EOL,$text);
	   $text = str_replace("<br/>",PHP_EOL,$text);
	   $text = str_replace("<br />",PHP_EOL,$text);
	   $text = utf8_decode($text);
	   $this->pdf->SetXY($this->x,$this->y);
// 	   $this->pdf->WriteTag($width,$height,$text,$border,$align,0,"1, 0, 0, 1");
	   $this->pdf->MultiCell($width,$height,$text,$border,$align);
	   $this->upX($width);
	   
	   // nombre de saut de ligne
	   preg_match_all("/".PHP_EOL."/",$text,$eol);
	   $upY = ((5*(count($eol[0])))+(floor(strlen($text)/120))*5);
	   return $upY;
	   
	   
    }
    /**
     * Affiche une image 
     * @param string $image url de l'image
     */
    public function afficheImage($image,$width="",$height=""){
	   $this->pdf->Image($image,$this->x,$this->y,$width,$height);
	   if($height!="") $this->upY($height);
	   else{
		  $size = getimagesize($image);
		  $height = $size[1];
		  // Ratio avec la taille d�finie
		  if($width != "") $height = (($width/$size[0])*$size[1]);
	   }
	   $this->upY($height+5);
    }
    /**
      * D�cale la position courrante verticalement
      * @param int $i d�calage en mm (default 5)
      */
    function upY($i=5){ $this->y += $i; }
    /**
      * D�cale la position courrante horizontalement
      * @param int $i d�calage en mm 
      */
    function upX($x){ $this->x += $x; }
    /**
      * Set la position Horizontale
      * @param int $x position
      */
    function setX($x){ $this->x = $x; }
    /**
      * Set la position Verticale
      * @param int $y position
      */
    function setY($y){ $this->y = $y; }
    /**
      * Set la largeur de l'�criture
      * @param int $w 
      */
    function setWidth($w){ $this->width = $w; }
    /**
      * Insert un saut de page
      */
    public function sautPage(){
	   $this->pdf->AddPage();
	   if($this->backgroundImage != "") $this->setBackground($this->backgroundImage);
    }
    /**
     * Affiche le pdf
     */
    public function output($name="",$type=""){
	   return $this->pdf->Output($name,$type);
    }
    /**
     * Construit un pdf a partir d'autres
     * @param array $aPDF tableau de pdf (url)
     */
    public function getFrom(array $aPDF){
	   $pdf = new FPDI();
	   foreach($aPDF as $filename){
		  if(!file_exists($filename)) throw new Exception ("Le fichier n'existe pas : ".$filename);
// 		  $perms = self::file_perms($filename);
// 		  print_r($perms);die;
		  try{
			 $nbpages = $pdf->setSourceFile($filename);
			 for($j=1;$j<=$nbpages;$j++){
				$template = $pdf->importPage($j, '/MediaBox');
				$pdf->addPage();
				$pdf->useTemplate($template, 0, 0);
			 }
		  }catch (Exception $e) {die("Error : ".$e->getMessage());}
	   }
	   $this->pdf = $pdf;
    }
    public static function copy($filename){
	   $pdf = new FPDI();
	   $nbpages = $pdf->setSourceFile($filename);
	   for($j=1;$j<=$nbpages;$j++){
		  $template = $pdf->importPage($j, '/MediaBox');
		  $pdf->addPage();
		  $pdf->useTemplate($template, 0, 0);
	   }
	   return $pdf;
    }
    private static function file_perms($file, $octal = false) {
	   if(!file_exists($file)) return false;
	   $perms = fileperms($file);
	   $cut = $octal ? 2 : 3;
	   return substr(decoct($perms), $cut);
    }
}
?>